import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;


/**
 * @Author XueZhimin
 * @PROJECT_NAME: Java_Lab001
 * @Description TODO gui 获取鼠标点击的坐标
 * @Date 2023/10/15 18:46
 * @Version 1.0
 */

public class MouseClickExample extends JFrame implements MouseListener {
    int kw = 0;
    int kh = 0;
    int sw = 0;
    int sh = 0;
    static String name;
    static BufferedImage screenCapture;
    Robot robot = null;
    Dimension scrSize=Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle rectangle;

    /**
     * 存文件
     * @param x
     * @param y
     * @param width
     * @param height
     * @throws AWTException
     */
    public void img(int x,int y,int width,int height) {
        if(width <= 0|| height<=0){

            width = scrSize.width;
            height = scrSize.height;
            x = 0;
            y = 0;

        }
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
            rectangle = new Rectangle(x,y,width,height);
            screenCapture = robot.createScreenCapture(rectangle);
        FileOutputStream fileOutputStream = null;
        Date date = new Date();
        long timestamp = date.getTime();
        name = "D:/"+timestamp+".jpg";

        try {
            fileOutputStream = new FileOutputStream(name);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fileOutputStream);
            encoder.encode(screenCapture);
        }catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        dispose();
    }
    /**
     * 面板
     */
    public  MouseClickExample() {
        addMouseListener(this);
        setUndecorated(true);
        setSize(scrSize.width,scrSize.height);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setBackground(new Color(239, 238, 238, 222));
        setVisible(true);
        setOpacity(0.01f);
    }
    /**
     *  单击触发事件
     * @param e
     */
    public void mouseClicked(MouseEvent e) {
        System.out.println("单机了");
    }

    /**
     *  按下触发事件
     * @param e
     */
    @Override
    public void mousePressed(MouseEvent e) {
        int x2 = e.getX();
        int y2 = e.getY();
        kw = x2;
        kh = y2;
        System.out.println("Mouse clicked at (" + x2 + ", " + y2 + ")");

    }

    /**
     * 释放触发事件
     * @param e
     */

    @Override
    public void mouseReleased(MouseEvent e) {
        int x3 = e.getX();
        int y3 = e.getY();
        sw = x3;
        sh = y3;
        System.out.println("Mouse clicked at (" + x3 + ", " + y3 + ")");
        if(sw-kw<10||sh-kh<10){
            this.dispose();
        }
        img(kw,kh,sw-kw,sh-kh);

        Pand.window.setVisible(true);
        new Wenjian().wen();
    }

    /**
     * 进入组件触发事件
     * @param e
     */
    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println(123);
    }

    /**
     *  离开组件触发事件
     * @param e
     */
    @Override
    public void mouseExited(MouseEvent e) {
        System.out.println("离开了");
    }

//    public static void main(String[] args) {
//        /**
//         * invokeLater一般用于在线程里修改swing组件的外观，因为swing组件是非同步的，所以不能在线程中直接修改，
//         * 会不同步，得不到期望的效果，所以要把修改外观的代码放在一个单独的线程中，交给invokeLater：
//         * 后面的程序(线程中的重写的run（）方法及run（）方法中的代码)和这个参数的线程(new Runnable())对象可以并行，异步地执行
//         */
//        SwingUtilities.invokeLater(new Runnable() {
//            public void run() {
//                new MouseClickExample();
//            }
//        });
//  }
}